<?php
//SELL_LO 7
//BUY_LO 8

class ControlMP
{
	//Use the rule increase or decrease price to change to price
	public function changePrice($price, $isIncrease)
	{
		$nu;
		if ($isIncrease)
		{
			if ($price >= 100)
			$nu = 1;
			else if ($price >= 50)
			$nu = 0.5;
			else
			$nu = 0.1;
		}
		else
		{
			if ($price >= 100)
			$nu = -1;
			else if ($price >= 50)
			$nu = -0.5;
			else
			$nu = -0.1;	
		}
		return $price + $nu;
	}

	//----------------------------------------------------------------------------------------------------------------------
	//set to sellLO table
	public function setToSellLO($stockid, $userid, $amount, $price)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		date_default_timezone_set('Asia/Bangkok');
		$date = date('Y-m-d H:i:s');
		$sql = "INSERT INTO sellLO (UserID, StockID, Price, Amount, Time) VALUES ('$userid','$stockid','$price','$amount','$date')";
		mysql_query($sql);
	}

	//action sellMP
	public function sellMP($stockid, $userid, $amount, $code)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlMoney.php';
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlStock.php';
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlStatistic.php';
		$startAmount = $amount;
		$controlMoney = new ControlMoney();
		$controlStock = new ControlStock();
		$price = 0;
		$price = 0;
		$totalprice = 0; 
		$totalamount = 0;
		$high = 0;
		$low = 0;
		$controlStatistic = new ControllStatistic();

		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM buylo WHERE (stockid ='$stockid') ORDER BY Price DESC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		while($row = mysql_fetch_array($result))
		{
			$amount_buy = intval($row['Amount']);
			//Subtraction of amount
			if ($amount >= $amount_buy)
			{
				date_default_timezone_set('Asia/Bangkok');
				$date = date('Y-m-d H:i:s');
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,'$userid',7,'$stockid',".$row['Price'].",'$amount_buy','$date')";
				mysql_query($sql);
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,".$row['UserID'].",8,'$stockid',".$row['Price'].",'$amount_buy','$date')";
				mysql_query($sql);
				$amount -= $amount_buy;
				$amount_buy = 0;
			}
			else
			{
				date_default_timezone_set('Asia/Bangkok');
				$date = date('Y-m-d H:i:s');
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,'$userid',7,'$stockid',".$row['Price'].",'$amount','$date')";
				mysql_query($sql);
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,".$row['UserID'].",8,'$stockid',".$row['Price'].",'$amount','$date')";
				mysql_query($sql);
				$amount_buy -= $amount;
				$amount = 0;
			}

			//Check the remain amount
			if ($amount_buy > 0)
			{
				$sql = "UPDATE buylo SET Amount='$amount_buy' WHERE UserID=".$row['UserID']." AND StockID='$stockid'";
				mysql_query($sql);
			}
			else
			{
				$sql = "DELETE FROM buylo WHERE UserID=".$row['UserID']." AND StockID='$stockid'";
				mysql_query($sql);
			}
			//Sub money in subuser, cho nguoi ban
			$controlMoney->subMainUserMoney($row['UserID'], (intval($row['Amount']) - $amount_buy) * floatval($row['Price']));
			//Add stock in subuser, cho nguoi ban
			$controlStock->addUserStock($row['UserID'], $stockid, (intval($row['Amount']) - $amount_buy));
			//Add money in user
			$controlMoney->addMainUserMoney($userid, (intval($row['Amount']) - $amount_buy) * floatval($row['Price']));
			//Statistic match
			$controlStatistic->addCurrentMatch($stockid, floatval($row['Price']), (intval($row['Amount']) - $amount_buy));
				
			$totalamount += intval($row['Amount']) - $amount_buy;
			$totalprice += (intval($row['Amount']) - $amount_buy) * floatval($row['Price']);
			if (floatval($row['Price']) > $high) $high = floatval($row['Price']);
			if (floatval($row['Price']) < $low || $low == 0) $low = floatval($row['Price']);
			if ($amount == 0) break;
			
			//Get current price
			$price = floatval($row['Price']);
			if ($amount == 0) break;
		}

		//Statistic price
		if ($nRow > 0)
		$controlStatistic->addCurrentPrice($stockid, $totalprice, $totalamount, $high, $low);
		
		//Sub stock in user
		$controlStock->subUserStock($userid, $stockid, ($startAmount - $amount));

		//Set to memory the action
		if ($amount > 0)
		{
			$controlStock->subUserStock($userid, $stockid, $startAmount);
			$this->setToSellLO($stockid, $userid, $amount, $this->changePrice($price, false));
		}
	}

	//----------------------------------------------------------------------------------------------------------------------------------------------

	//set to BuyLO table
	public function setToBuyLO($stockid, $userid, $amount, $price)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		date_default_timezone_set('Asia/Bangkok');
		$date = date('Y-m-d H:i:s');
		$sql = "INSERT INTO buyLO (UserID, StockID, Price, Amount, Time) VALUES ('$userid','$stockid','$price','$amount','$date')";
		mysql_query($sql);
	}

	//action buyMP
	public function buyMP($stockid, $userid, $amount, $code)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlMoney.php';
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlStock.php';
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlStatistic.php';
		$startAmount = $amount;
		$controlMoney = new ControlMoney();
		$controlStock = new ControlStock();
		$price = 0;
		$totalprice = 0; 
		$totalamount = 0;
		$high = 0;
		$low = 0;
		$controlStatistic = new ControllStatistic();

		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM selllo WHERE (stockid ='$stockid') ORDER BY Price ASC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		while($row = mysql_fetch_array($result))
		{
			$amount_sell = intval($row['Amount']);
			//Subtraction of amount
			if ($amount >= $amount_sell)
			{
				date_default_timezone_set('Asia/Bangkok');
				$date = date('Y-m-d H:i:s');
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,'$userid',8,'$stockid',".$row['Price'].",'$amount_sell','$date')";
				mysql_query($sql);
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,".$row['UserID'].",7,'$stockid',".$row['Price'].",'$amount_sell','$date')";
				mysql_query($sql);
				$amount -= $amount_sell;
				$amount_sell = 0;
			}
			else
			{
				date_default_timezone_set('Asia/Bangkok');
				$date = date('Y-m-d H:i:s');
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,'$userid',8,'$stockid',".$row['Price'].",'$amount','$date')";
				mysql_query($sql);
				$sql = "INSERT INTO userorder (ID, UserID, OrderID, StockID, Price, Amount, Time) VALUES (null,".$row['UserID'].",7,'$stockid',".$row['Price'].",'$amount','$date')";
				mysql_query($sql);
				$amount_sell -= $amount;
				$amount = 0;
			}
			//Check the remain amount
			if ($amount_sell > 0)
			{
				$sql = "UPDATE selllo SET Amount='$amount_sell' WHERE UserID=".$row['UserID']." AND StockID='$stockid'";
				mysql_query($sql);
			}
			else
			{
				$sql = "DELETE FROM selllo WHERE UserID=".$row['UserID']." AND StockID='$stockid'";
				mysql_query($sql);
			}
			//Add money in subuser, cho nguoi mua
			$controlMoney->addMainUserMoney($row['UserID'], (intval($row['Amount']) - $amount_sell) * floatval($row['Price']));
			//Sub stock in subuser, cho nguoi mua
			$controlStock->subUserStock($row['UserID'], $stockid, (intval($row['Amount']) - $amount_sell));
			//Sub money in user
			$controlMoney->subMainUserMoney($userid, (intval($row['Amount']) - $amount_sell) * floatval($row['Price']));
			//Statistic match
			$controlStatistic->addCurrentMatch($stockid, floatval($row['Price']), (intval($row['Amount']) - $amount_sell));
			
			$totalamount += intval($row['Amount']) - $amount_sell;
			$totalprice += (intval($row['Amount']) - $amount_sell) * floatval($row['Price']);
			if (floatval($row['Price']) > $high) $high = floatval($row['Price']);
			if (floatval($row['Price']) < $low || $low == 0) $low = floatval($row['Price']);
			
			//Get current price
			$price = floatval($row['Price']);
			if ($amount == 0) break;
		}
		
		//Statistic price
		if ($nRow > 0)
		$controlStatistic->addCurrentPrice($stockid, $totalprice, $totalamount, $high, $low);
		
		//Add stock in user
		$controlStock->addUserStock($userid, $stockid, ($startAmount - $amount));
		//Set to memory the action
		if ($amount > 0)
		{
			$controlMoney->subMainUserMoney($userid, $amount * $this->changePrice($price, true));
			$this->setToBuyLO($stockid, $userid, $amount, $this->changePrice($price, true));
		}
	}
}
?>